<?php
/**
 * Created by PhpStorm.
 * User: Silence
 * Date: 2017-07-16
 * Time: 9:59
 */

namespace App\Http\Controllers\AreaMaster;

use App\Base\BaseFunc;
use App\Base\Power;
use App\Base\TaskState;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;

class AreaTaskController extends Controller
{
    public function searchTask()
    {
        $data['area'] = DB::table('area')
            ->where('area_id','=',session('employee_area'))
            ->first();
        $data['nowArea'] = $data['area']->area_name; //获取当前区域名称

        $data['areaData'] = DB::table('area')->get();
        $data['moneyData'] =DB::table('task')
            ->where('task_area','=',session('employee_area'))
            ->leftJoin('area','area_id','=','task_area')
            ->leftJoin('task_state','task_state_id','=','task_state')
            ->leftJoin('customer','customer_id','=','task_customer')
            ->get();

        return view("/areaMaster.searchTask",$data);
    }

    public function searchTasks($task_id,Power $power)
    {
        $data['employee'] = DB::table('employee_re_task')
            ->where('task_id','=',$task_id)
            ->leftJoin('employee','employee.employee_id','=','employee_re_task.employee_id')
            ->first();   //获得领取当前任务的人
        $data['employeeData'] = DB::table('employee')
            ->where('employee_area','=',session('employee_area'))
            ->where('employee_power','=',$power->yuangong())
            ->where('employee_state','=',1)
            ->get();  //获取当前区域普通员工
        if($data['employee'] == NULL)
        {
            $data['nowTask'] = DB::table('task')
                ->where('task_id','=',$task_id)
                ->first();  //获得当前任务
            $data['employee_name'] = "暂无";
            $data['employee_phone'] = "暂无";
            return view('/areaMaster.searchTasks',$data);
        }
        else
        {
            $data['nowTask'] = DB::table('task')
                ->where('task_id','=',$task_id)
                ->first();  //获得当前任务
            $data['employee'] = DB::table('employee_re_task')
                ->where('task_id','=',$task_id)
                ->leftJoin('employee','employee.employee_id','=','employee_re_task.employee_id')
                ->first();   //获得领取当前任务的人
            $data['employee_name'] = $data['employee']->employee_name;
            $data['employee_phone'] = $data['employee']->employee_phone;
            $data['evaluationData'] = DB::table('evaluation')
                ->where('task_id','=',$task_id)
                ->leftJoin('customer','customer.customer_id','=','evaluation.customer_id')
                ->get();
            return view('/areaMaster.searchTasks',$data);
        }
    }

    public function taskToEmployee(BaseFunc $baseFunc,TaskState $taskState)
    {
        $input = Input::only('employee_id','task_id');
        $newState['task_state'] = $taskState->stating()->task_state_id;
        $stating = DB::table('employee_re_task')->insert($input);
        if($stating)
        {
            DB::table('task')->where('task_id','=',$input['task_id'])->update($newState);
            $baseFunc->setRedirectMessage(true,"任务指派成功！",NULL,NULL);
            return redirect()->back();
        }
        else
        {
            $baseFunc->setRedirectMessage(false,"任务指派失败！",NULL,NULL);
            return redirect()->back();
        }
    }
}